package cn.dubito.miaosha.redis;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author liliang
 * @description:
 * @date 2018/3/29.
 */
@Service
public class RedisService {

    @Autowired
    private JedisPool jedisPool;

    /**
     * 获取对象
     *
     * @param prefix
     * @param key
     * @param clazz
     * @param <T>
     * @return
     */
    public <T> T get(KeyPrefix prefix, String key, Class<T> clazz) {
        Jedis resource = null;
        try {
            resource = jedisPool.getResource();
            String realKey = prefix.getPrefix() + key;
            String str = resource.get(realKey);
            return stringToBean(str, clazz);
        } finally {
            returnToPool(resource);
        }
    }

    /**
     * 设置对象
     *
     * @param prefix
     * @param key
     * @param value
     * @param <T>
     * @return
     */
    public <T> boolean set(KeyPrefix prefix, String key, T value) {
        Jedis resource = null;
        try {
            resource = jedisPool.getResource();
            String str = beanToString(value);
            if (str == null || str.length() <= 0) {
                return false;
            }
            String realKey = prefix.getPrefix() + key;
            int seconds = prefix.expireSeconds();
            if (seconds <= 0) {
                resource.set(realKey, str);
            } else {
                resource.setex(realKey,seconds,str);
            }
            return true;
        } finally {
            returnToPool(resource);
        }
    }


    /**
     * 增加值
     *
     * @param prefix
     * @param key
     * @param <T>
     * @return
     */
    public <T> Long incr(KeyPrefix prefix, String key) {
        Jedis resource = null;
        try {
            resource = jedisPool.getResource();
            String realKey = prefix.getPrefix() + key;
            return resource.incr(realKey);
        } finally {
            returnToPool(resource);
        }
    }

    /**
     * 减少值
     * @param prefix
     * @param key
     * @param <T>
     * @return
     */
    public <T> Long decr(KeyPrefix prefix, String key) {
        Jedis resource = null;
        try {
            resource = jedisPool.getResource();
            String realKey = prefix.getPrefix() + key;
            return resource.decr(realKey);
        } finally {
            returnToPool(resource);
        }
    }


    /**
     * 判断 key 是否存在
     *
     * @param prefix
     * @param key
     * @param <T>
     * @return
     */
    public <T> boolean exist(KeyPrefix prefix, String key) {
        Jedis resource = null;
        try {
            resource = jedisPool.getResource();
            String realKey = prefix.getPrefix() + key;
            return resource.exists(realKey);
        } finally {
            returnToPool(resource);
        }
    }

    /**
     * Bean 对象转字符串
     *
     * @param value
     * @param <T>
     * @return
     */
    public <T> String beanToString(T value) {
        if (value == null) {
            return null;
        }
        Class<?> clazz = value.getClass();
        if (clazz == int.class || clazz == Integer.class) {
            return String.valueOf(value);
        } else if (clazz == String.class) {
            return value.toString();
        } else if (clazz == long.class || clazz == Long.class) {
            return String.valueOf(value);
        } else {
            return JSON.toJSONString(value);
        }
    }


    /**
     * 字符串转 bean 对象
     *
     * @param str
     * @param clazz
     * @param <T>
     * @return
     */
    @SuppressWarnings("unchecked")
    public <T> T stringToBean(String str, Class<T> clazz) {
        if (str == null || str.length() <= 0 || clazz == null) {
            return null;
        }
        if (clazz == int.class || clazz == Integer.class) {
            return (T) Integer.valueOf(str);
        } else if (clazz == String.class) {
            return (T) str;
        } else if (clazz == long.class || clazz == Long.class) {
            return (T) Long.valueOf(str);
        } else {
            return JSON.toJavaObject(JSON.parseObject(str), clazz);
        }
    }

    private void returnToPool(Jedis resource) {
        if (resource != null) {
            resource.close();
        }
    }

    /**
     * 删除缓存
     * @param prefix
     * @param key
     * @return
     */
    public boolean delete(KeyPrefix prefix, String key) {
        Jedis resource = null;
        try {
            resource = jedisPool.getResource();
            String realKey = prefix.getPrefix() + key;
            return resource.del(realKey) > 0;
        } finally {
            returnToPool(resource);
        }
    }
}
